package Fisica.Configuracion;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Inteface.InterfaceFisica;
import Modelo.Ajuste;
import Modelo.Toner;

public class DAOAjuste {
	public static String SELECT = "SELECT * FROM `ajuste` WHERE `efecto` = ? and `ajuste` = ?;";
	
	public static String FIRST = "SELECT * FROM `ajuste` order by `id` asc";
	public static String LAST = "SELECT * FROM `ajuste` order by `id` desc";
	public static String PRE = "SELECT * FROM `ajuste` WHERE `id` < ? order by `id` desc";
	public static String POST = "SELECT * FROM `ajuste` WHERE `id` > ? order by `id` asc";
	
	
	public boolean insert(Object obj) { //ok
		Ajuste v = (Ajuste)obj;
		
		//INSERT INTO `Ajuste` (`marca`, `modelo`, `codigo`) VALUES ("HP", "A12","A1234");
		String INSERTAR = "INSERT INTO `Ajuste` (`efecto`, `ajuste`) VALUES (?, ?);";


		try {			
			PreparedStatement st;
			ResultSet rs;
			//INSERTO
			st = ConexionVieja.getConexion().prepareStatement(INSERTAR);	
			st.setInt(1, v.getEfecto());
			st.setString(2, v.getAjuste());
			st.executeUpdate();
			//RECUPERO
			st = ConexionVieja.getConexion().prepareStatement(SELECT);
			st.setInt(1, v.getEfecto());
			st.setString(2, v.getAjuste());
			rs = st.executeQuery();
			if(rs.next())
				v.setId(rs.getInt("id"));
			System.out.println("inserto: "+v.getId());

			return true;
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
			
	}
	
	public boolean update(Object obj){ //ok
		Ajuste v = (Ajuste)obj;
		
		String UPDATE = "UPDATE `Ajuste` SET `efecto` = ?, `ajuste` = ? "
		+ "WHERE `id` = ?;";


		try {			
			PreparedStatement st;		
			ResultSet rs;
			//INSERTO
			st = ConexionVieja.getConexion().prepareStatement(UPDATE);
			st.setInt(1, v.getEfecto());
			st.setString(2, v.getAjuste());
			st.setInt(3, v.getId());
			st.executeUpdate();
			//RECUPERO
			st = ConexionVieja.getConexion().prepareStatement(SELECT);
			st.setInt(1, v.getEfecto());
			st.setString(2, v.getAjuste());
			rs = st.executeQuery();
			if(rs.next())
				v.setId(rs.getInt("id"));
			System.out.println("actualizo: "+v.getId());

			return true;
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
			
	}
	
	
	public boolean delete(Object obj){ //ok
		Ajuste v = (Ajuste)obj;
		String delete = "delete from `Ajuste` where `id` = ?;";
		String select = "select * from `Ajuste` where `id` = ?;";
		try {			
			PreparedStatement st;
			ResultSet rs;
			st = ConexionVieja.getConexion().prepareStatement(delete);	
			st.setInt(1, v.getId());
			st.executeUpdate();
			
			st = ConexionVieja.getConexion().prepareStatement(select);	
			st.setInt(1, v.getId());
			rs = st.executeQuery();
			
			System.out.println("inserto: "+v.getId());

			return !rs.next();
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
	}
	
	public List<Object> list (){ //ok
		List<Object> lista = new ArrayList();	
		String sql = "select * from `Ajuste`;";
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			
			while (rs.next()){
				Ajuste o = new Ajuste(); 
				o.setId(rs.getInt("id")); 				
				o.setEfecto(rs.getInt("efecto"));				
				o.setAjuste(rs.getString("ajuste"));					
				
				lista.add(o);
				}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return lista;		
	}

	public Object getById(int id){ //ok
		String sql = "select * from `Ajuste` where `id` = ?;";
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(sql);	
			st.setInt(1, id);
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Ajuste o = new Ajuste(); 
				o.setId(rs.getInt("id")); 				
				o.setEfecto(rs.getInt("efecto"));				
				o.setAjuste(rs.getString("ajuste"));
				
				return o;
				}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return new Ajuste();	
	}

	

	public static void main(String[] args) {
		Ajuste t = new Ajuste();
		t.setAjuste("roto");
		t.setEfecto(23);
		
		DAOAjuste daot = new DAOAjuste();
//		daot.insert(t); //ok
		Ajuste t2 = (Ajuste) daot.getById(5); //ok
		System.out.println("efecto: "+t2.getEfecto());
		t2.setEfecto(15);
//		if (daot.update(t2)) //ok
//			System.out.println("EXITO");
//		else
//			System.out.println("FRACASO");
		
//		List<Object> l = daot.list(); // ok
//		Ajuste y;
//		for(Object x: l){
//			y = (Ajuste)x;
//			System.out.println("id: "+y.getId());
//			System.out.println("ajuste: "+y.getAjuste());
//			System.out.println("efecto: "+y.getEfecto());
//			
//		}
		
//		if (daot.delete(t2)) //ok
//			System.out.println("EXITO");
//		else
//			System.out.println("FRACASO");
		
		
	}

	public Object getObject (PreparedStatement st){ //ok
		Ajuste o = new Ajuste(); 	
		
		try {
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				o.setId(rs.getInt("id")); 				
				o.setEfecto(rs.getInt("efecto"));				
				o.setAjuste(rs.getString("ajuste"));				
			}	
			return o;
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return new Toner();		
	}
	
	@Override
	public Object getPre(int id) {
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(PRE);
			st.setInt(1, id);
			return getObject(st);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public Object getPost(int id) {
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(POST);
			st.setInt(1, id);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public Object getFirst() {
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(FIRST);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public Object getLast() {
		try {
			PreparedStatement st = ConexionVieja.getConexion().prepareStatement(LAST);
			return getObject(st);			
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}
}
